<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 28] 28.23 java.net.URLConnection (JDK 1.0)</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 16:31:22 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch28_22.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 28<br>The java.net Package</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch28_24.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-28-SECT-23">28.23 java.net.URLConnection (JDK 1.0)</A></h2>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-28-SECT-23.1"></A></h3>
</DIV>

<P CLASS=para>
This abstract class defines a network connection to an
object specified by a URL. <tt CLASS=literal>URL.openConnection()</tt>
returns a <tt CLASS=literal>URLConnection</tt> instance. You would use a
<tt CLASS=literal>URLConnection</tt> object when you want more control over
the downloading of data than is available through the
simpler <tt CLASS=literal>URL</tt> methods.

<P CLASS=para>
<tt CLASS=literal>connect()</tt> actually performs the network connection.
Other methods that depend on being connected will call this
method. <tt CLASS=literal>getContent()</tt> returns the data referred to
by the <tt CLASS=literal>URL</tt>, parsed into an appropriate type of
<tt CLASS=literal>Object</tt>. If the URL protocol supports read and write
operations, then <tt CLASS=literal>getInputStream()</tt> and
<tt CLASS=literal>getOutputStream()</tt> respectively return input and
output streams to the object referred to by the URL.

<P CLASS=para>
<tt CLASS=literal>getContentLength()</tt>, <tt CLASS=literal>getContentType()</tt>,
<tt CLASS=literal>getContentEncoding()</tt>, <tt CLASS=literal>getExpiration()</tt>,
<tt CLASS=literal>getDate()</tt>, and <tt CLASS=literal>getLastModified()</tt>
return the appropriate information about the object referred
to by the URL, if that information can be determined
(e.g., from HTTP header fields). <tt CLASS=literal>getHeaderField()</tt>
returns an HTTP header field specified by name or by number.
<tt CLASS=literal>getHeaderFieldInt()</tt> and <tt CLASS=literal>getHeaderFieldDate()</tt>
return the value of a named header field parsed as an
integer or a date.

<P CLASS=para>
There are a number of options that you may specify to
control how the <tt CLASS=literal>URLConnection</tt> behaves. These
options are set with the various <tt CLASS=literal>set()</tt> methods, and may be
queried with corresponding <tt CLASS=literal>get()</tt> methods. The
options must be set before the <tt CLASS=literal>connect()</tt> method is
called.
<tt CLASS=literal>setDoInput()</tt> and <tt CLASS=literal>setDoOutput()</tt>
allow you to specify whether you  use the
<tt CLASS=literal>URLConnection</tt> for input and/or output. The default
is input-only.  
<tt CLASS=literal>setAllowUserInteraction()</tt> specifies whether
user interaction (such as typing a password) is allowed
during the data transfer. The initial default is
<tt CLASS=literal>false</tt>. <tt CLASS=literal>setDefaultAllowUserInteraction()</tt> is a
class method that allows you to change the default value
for user interaction.
<tt CLASS=literal>setUseCaches()</tt> allows you to specify whether a
cached version of the URL may be used. You can set this to
<tt CLASS=literal>false</tt> to force a URL to be reloaded.
<tt CLASS=literal>setDefaultUseCaches()</tt> sets the default value for
<tt CLASS=literal>setUseCaches()</tt>. 
<tt CLASS=literal>setIfModifiedSince()</tt> allows you to specify that a
URL should not be fetched (if it is possible to determine
its modification date) unless it has been modified since a
specified time.

<DIV CLASS=screen>
<P>
<PRE>
public abstract class <tt CLASS=literal>URLConnection</tt> extends Object {
    // <tt CLASS=literal>Protected Constructor</tt>
            protected <tt CLASS=literal>URLConnection</tt>(URL <tt CLASS=literal>url</tt>);
    // <tt CLASS=literal>Class Variables</tt>
        <b>1.1</b>public static FileNameMap <tt CLASS=literal>fileNameMap</tt>;
    // <tt CLASS=literal>Protected Instance Variables</tt>
            protected boolean <tt CLASS=literal>allowUserInteraction</tt>;
            protected boolean <tt CLASS=literal>connected</tt>;
            protected boolean <tt CLASS=literal>doInput</tt>;
            protected boolean <tt CLASS=literal>doOutput</tt>;
            protected long <tt CLASS=literal>ifModifiedSince</tt>;
            protected URL <tt CLASS=literal>url</tt>;
            protected boolean <tt CLASS=literal>useCaches</tt>;
    // <tt CLASS=literal>Class Methods</tt>
            public static boolean <tt CLASS=literal>getDefaultAllowUserInteraction</tt>();
            public static String <tt CLASS=literal>getDefaultRequestProperty</tt>(String <tt CLASS=literal>key</tt>);
            protected static String <tt CLASS=literal>guessContentTypeFromName</tt>(String <tt CLASS=literal>fname</tt>);
            public static String <tt CLASS=literal>guessContentTypeFromStream</tt>(InputStream <tt CLASS=literal>is</tt>) throws IOException;
            public static synchronized void <tt CLASS=literal>setContentHandlerFactory</tt>(ContentHandlerFactory <tt CLASS=literal>fac</tt>);
            public static void <tt CLASS=literal>setDefaultAllowUserInteraction</tt>(boolean <tt CLASS=literal>defaultallowuserinteraction</tt>);
            public static void <tt CLASS=literal>setDefaultRequestProperty</tt>(String <tt CLASS=literal>key</tt>, String <tt CLASS=literal>value</tt>);
    // <tt CLASS=literal>Public Instance Methods</tt>
            public abstract void <tt CLASS=literal>connect</tt>() throws IOException;
            public boolean <tt CLASS=literal>getAllowUserInteraction</tt>();
            public Object <tt CLASS=literal>getContent</tt>() throws IOException;
            public String <tt CLASS=literal>getContentEncoding</tt>();
            public int <tt CLASS=literal>getContentLength</tt>();
            public String <tt CLASS=literal>getContentType</tt>();
            public long <tt CLASS=literal>getDate</tt>();
            public boolean <tt CLASS=literal>getDefaultUseCaches</tt>();
            public boolean <tt CLASS=literal>getDoInput</tt>();
            public boolean <tt CLASS=literal>getDoOutput</tt>();
            public long <tt CLASS=literal>getExpiration</tt>();
            public String <tt CLASS=literal>getHeaderField</tt>(String <tt CLASS=literal>name</tt>);
            public String <tt CLASS=literal>getHeaderField</tt>(int <tt CLASS=literal>n</tt>);
            public long <tt CLASS=literal>getHeaderFieldDate</tt>(String <tt CLASS=literal>name</tt>, long <tt CLASS=literal>Default</tt>);
            public int <tt CLASS=literal>getHeaderFieldInt</tt>(String <tt CLASS=literal>name</tt>, int <tt CLASS=literal>Default</tt>);
            public String <tt CLASS=literal>getHeaderFieldKey</tt>(int <tt CLASS=literal>n</tt>);
            public long <tt CLASS=literal>getIfModifiedSince</tt>();
            public InputStream <tt CLASS=literal>getInputStream</tt>() throws IOException;
            public long <tt CLASS=literal>getLastModified</tt>();
            public OutputStream <tt CLASS=literal>getOutputStream</tt>() throws IOException;
            public String <tt CLASS=literal>getRequestProperty</tt>(String <tt CLASS=literal>key</tt>);
            public URL <tt CLASS=literal>getURL</tt>();
            public boolean <tt CLASS=literal>getUseCaches</tt>();
            public void <tt CLASS=literal>setAllowUserInteraction</tt>(boolean <tt CLASS=literal>allowuserinteraction</tt>);
            public void <tt CLASS=literal>setDefaultUseCaches</tt>(boolean <tt CLASS=literal>defaultusecaches</tt>);
            public void <tt CLASS=literal>setDoInput</tt>(boolean <tt CLASS=literal>doinput</tt>);
            public void <tt CLASS=literal>setDoOutput</tt>(boolean <tt CLASS=literal>dooutput</tt>);
            public void <tt CLASS=literal>setIfModifiedSince</tt>(long <tt CLASS=literal>ifmodifiedsince</tt>);
            public void <tt CLASS=literal>setRequestProperty</tt>(String <tt CLASS=literal>key</tt>, String <tt CLASS=literal>value</tt>);
            public void <tt CLASS=literal>setUseCaches</tt>(boolean <tt CLASS=literal>usecaches</tt>);
            public String <tt CLASS=literal>toString</tt>();  //<tt CLASS=literal> Overrides Object</tt>
}
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-28-SECT-23.2">Extended By:</A></h3>

<P CLASS=para>
HttpURLConnection

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-28-SECT-23.3">Passed To:</A></h3>

<P CLASS=para>
ContentHandler.getContent()

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-28-SECT-23.4">Returned By:</A></h3>

<P CLASS=para>
URL.openConnection(), 
URLStreamHandler.openConnection()

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch28_22.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch28_24.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>java.net.URL (JDK 1.0)</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>java.net.URLEncoder (JDK 1.0)</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
